﻿using System;
using System.Collections.Generic;
using System.Linq;

public static class KickQueueManager
{
    private static readonly SortedList<DateTime, string> _kickQueue = new SortedList<DateTime, string>();
    private static readonly object _queueLock = new object();

    // 添加物料到踢料队列
    public static void AddToKickQueue(string materialId, DateTime creationTime)
    {
        lock (_queueLock)
        {
            // 检查物料是否已经在队列中
            if (_kickQueue.Values.Contains(materialId))
            {
                Console.WriteLine($"警告: 物料{materialId}已在踢料队列中，跳过重复添加");
                return;
            }

            DateTime key = creationTime.AddTicks(_kickQueue.Count);
            _kickQueue.Add(key, materialId);
            Console.WriteLine($"添加物料到踢料队列: {materialId}, 创建时间: {creationTime:HH:mm:ss.fff}");
            LogQueueStatus();
        }
    }

    // 从踢料队列移除物料
    public static void RemoveFromKickQueue(string materialId)
    {
        lock (_queueLock)
        {
            var item = _kickQueue.FirstOrDefault(x => x.Value == materialId);
            if (!item.Equals(default(KeyValuePair<DateTime, string>)))
            {
                _kickQueue.Remove(item.Key);
                Console.WriteLine($"从踢料队列移除物料: {materialId}");
                LogQueueStatus();
            }
            else
            {
                Console.WriteLine($"警告: 物料{materialId}不在踢料队列中，无法移除");
            }
        }
    }

    // 获取下一个需要踢料的物料
    public static string GetNextKickMaterial()
    {
        lock (_queueLock)
        {
            if (_kickQueue.Count > 0)
            {
                // 验证物料是否需要踢料
                string materialId = _kickQueue.Values[0];
                var material = MaterialTracker.GetMaterialInfo(materialId);

                if (material != null && material.NeedKick && !material.IsKicked)
                {
                    return materialId;
                }
                else
                {
                    Console.WriteLine($"物料{materialId}不需要踢料或已踢料，从队列移除");
                    RemoveFromKickQueue(materialId);
                    return GetNextKickMaterial(); // 递归获取下一个
                }
            }
            return null;
        }
    }

    // 检查物料是否在踢料队列中
    public static bool IsInKickQueue(string materialId)
    {
        lock (_queueLock)
        {
            return _kickQueue.Values.Contains(materialId);
        }
    }

    // 获取踢料队列状态
    public static string GetQueueStatus()
    {
        lock (_queueLock)
        {
            return $"踢料队列深度: {_kickQueue.Count}";
        }
    }

    // 获取踢料队列快照
    public static List<string> GetQueueSnapshot()
    {
        lock (_queueLock)
        {
            return new List<string>(_kickQueue.Values);
        }
    }

    // 清空踢料队列
    public static void ClearQueue()
    {
        lock (_queueLock)
        {
            _kickQueue.Clear();
            Console.WriteLine("踢料队列已清空");
        }
    }

    private static void LogQueueStatus()
    {
        Console.WriteLine($"当前踢料队列深度: {_kickQueue.Count}");
    }
}